{extend name="public:layout" /}
{block name="title"}{$moudleName}列表{/block}
{block name="head"}{/block}
{block name="body"}
<div class="wrapper wrapper-content">
    <div class="ibox">
        <div class="ibox-title">
            <ol class="breadcrumb">
                <ol class="breadcrumb">
                    <li class="active"><i class="fa fa-hand-o-right"></i> {$moudleName}列表</li>
                </ol>
            </ol>
        </div>
        <div class="ibox-content">
            <div id="buttons">
                <button type="button" onclick="add();" class="btn btn-primary">
                    <i class="fa fa-sort"></i> 添加{$moudleName}
                </button>
                <button type="button" onclick="upOrder();" class="btn btn-info">
                    <i class="fa fa-sort"></i> 排序
                </button>
                <button type="button" onclick="batchDel(CheckboxValue())" class="btn btn-danger">
                    <i class="fa fa-trash"></i> 删除
                </button>
            </div>
            <table class="table table-bordered" id="grid"></table>
        </div>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
    $(function () {
        //列定义
        var columnsData=[
            {field: 'state', checkbox:true,},
            {field: 'aid', width:80, sortable:true, title: '编号'},
            {field: 'driver_name', title: '请假人员'},
            {field: 'title', title: '请假原因'},
            {field: 'leave_start_time', title: '起始时间'},
            {field: 'leave_end_time', title: '结束时间'},
            {field: 'leave_status', title: '当前状态',sortable:true,
                formatter:function (value,row,index) {
                    var arr=['<span class="label label-warning">待审批</span>','<span class="label label-primary">已同意</span>','<span class="label label-danger">驳回</span>','<span class="label label-success">已销假</span>'];
                    return arr[value];
                }},
            {field: 'operate', title: '操作', width:200, align: 'center',
                formatter:function (value,row,index) {
                    var html='';
                    if(row.leave_status==0){
                        html+='<a href="javascript:;" onclick="applyAction(this)" data-status="1" data-aid="'+row.aid+'" data-driver="'+row.leave_uid+'" class="btn btn-success btn-xs">同意</a> ';
                        html+='<a href="javascript:;" onclick="applyAction(this)" data-status="2" data-aid="'+row.aid+'" class="btn btn-danger btn-xs">驳回</a> ';
                    }
                    if(row.leave_status==1){
                        html+='<a href="javascript:;" onclick="applyAction(this)" data-status="3" data-aid="'+row.aid+'" data-driver="'+row.leave_uid+'" data-endtime="'+row.leave_end_time+'" class="btn btn-success btn-xs">销假</a> ';
                    }
                    return html;
                }
            }
        ];
        initTable('/system/logic.query/leaveList.html?mid={$Request.param.mid}',columnsData,function (params) {
            params.op="{$Request.param.op}";
            params.car_sn=$("input[name='car_sn']").val();
            params.car_status=$("select[name='car_status']").val();
            return params;
        })
    });
    //刷新表格
    function refresh() {
        $('#grid').bootstrapTable('refresh');
    }
    //添加
    function add() {
        var mid="{$Request.param.mid}";
        location.href="__URL__/add.html?mid="+mid;
    }
    //批量删除文章
    function batchDel(aid) {
        if (IsMore(aid)) {
            var url = "{:url('del')}";
            var params = {'aid': aid, 'mid': "{$Request.param.mid}"};
            doConfirm('你确认要删除吗？', url, params, function (data) {
                if (data.code == 1) {
                    rePage();
                }
            });
        }
    }
    //更新排序
    function upOrder() {
        if ($(".batchSort").length == 0) {
            alert('您还没有添加内容！');
            return false;
        }
        var params = $(".batchSort").serialize();
        var url = "__URL__/uporder.html?mid={$Request.param.mid}";
        getAjax(url, params, function (data) {
            if (data.code == 1) {
                rePage();
            }
        });
    }
    /*更新请假状态*/
    function applyAction(obj){
        In('layer',function () {
            var leave_status=$(obj).data('status');
            var leave_end_time=$(obj).data('endtime');
            var driver_uid=$(obj).data('driver');
            var data={aid:$(obj).data('aid'),mid:12,is_update:1,leave_status:leave_status};
            var url="{:url('logic.query/infoRecord')}";
            var driver_data={mid:16,is_update:1,driver_uid:driver_uid}
            /*同意*/
            if(leave_status==1){
                layer.confirm('确认审核通过吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    $.post(url,data,function (res) {
                        if(res.code==1){
                            driver_data.dri_status=1;
                            $.post(url,driver_data,function (data) {
                                if(data.code==1){
                                    layer.msg('审核通过',{time:1000},function () {
                                        refresh();
                                    })
                                }
                            })
                        }else{
                            layer.msg(res.msg,{time:1000});
                        }
                    })
                });
            }
            /*驳回*/
            if(leave_status==2){
                layer.prompt({title: '请输入驳回原因', formType: 2}, function(text, index){
                    data.reject_reason=text;
                    $.post(url,data,function (res) {
                        if(res.code==1){
                            layer.msg('请假已驳回',{time:1000},function () {
                                refresh();
                            })
                        }else{
                            layer.msg(res.msg,{time:1000});
                        }
                    })
                });

            }
            /*销假*/
            if(leave_status==3){
                var html='<div class="p-xl">' +
                    '<input class="form-control laydatetime" type="text" placeholder="请选择销假日期" id="changedate" name="leave_end_time" value="'+leave_end_time+'">' +
                    '</div>';
                layer.open({
                    type: 1,
                    title:'选择销假时间',
                    skin: 'layui-layer-rim', //加上边框
                    //area: ['420px', '300px'], //宽高
                    content:html,
                    btn:['确定销假','取消'],
                    yes:function () {
                        data.leave_end_time=$('input[name="leave_end_time"]').val();
                        $.post(url,data,function (res) {
                            if(res.code==1){
                                driver_data.dri_status=2;
                                $.post(url,driver_data,function (data) {
                                    if(data.code==1){
                                        layer.msg('销假成功',{time:1000},function () {
                                            layer.closeAll();
                                            refresh();
                                        })
                                    }
                                })
                            }else{
                                layer.msg(res.msg,{time:1000});
                            }
                        })

                    }
                });
            }
            In('laydate',function () {
                laydate.render({
                    elem: '#changedate'
                    ,type: 'datetime'
                    ,min: 0 //7天前
                });
            })
        })
    }
</script>
{/block}